package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.Children;
import com.matt.foundation.constant.ChildrenConstant;
import com.matt.foundation.constant.RequestConstant;

import com.matt.commons.model.StatusResponse;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.ChildrenCourseQuery;
import com.matt.foundation.vo.ChildrenCourseListVo;
import com.matt.foundation.vo.CourseChangeRecordVo;
import com.matt.foundation.vo.course.WebCoursePlanDetailVo;
import com.matt.service.ChildrenCourseService;
import com.matt.service.ChildrenService;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/7
 * *******************************
 */
@RestController
@RequestMapping("/web/children_course")
public class WebChildrenCourseController {

    @Autowired
    private ChildrenCourseService childrenCourseService;

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "查询宝宝课程",httpMethod = "POST")
    @RequestMapping("/page")
    public StatusResponse page(ChildrenCourseQuery query) {
        Page<ChildrenCourseListVo> childrenCoursePage = childrenCourseService.getChildrenCoursePageByCondition(query);
        return StatusResponse.success(childrenCoursePage);
    }

    @ApiOperation(value = "到修改宝宝排课",httpMethod = "POST")
    @RequestMapping("/modify_plan_detail")
    public StatusResponse modifyPlanDetail(String areaId, String childrenId, String childrenCourseId) {
        Map response = childrenCourseService.modifyPlanDetail(areaId, childrenId, childrenCourseId);
        return StatusResponse.success(response);
    }

    @ApiOperation(value = "获取课程计划详情",httpMethod = "POST")
    @RequestMapping("/get_plan_info")
    public StatusResponse getPlanInfo(@RequestHeader(RequestConstant.TICKET) String ticket, String coursePlanId) {
        StaffDTO staff = cache.getStaff(ticket);
        WebCoursePlanDetailVo vo = childrenCourseService.getWebCoursePlanDetail(staff, coursePlanId);
        return StatusResponse.success(vo);
    }

    @ApiOperation(value = "修改剩余课时",httpMethod = "POST")
    @RequestMapping("/modify_left_course")
    public StatusResponse modifyLeftCourseTime(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenId, String remark, Integer times) {
        Children children = childrenService.getById(childrenId);
        if (children.getStatus() != null && (children.getStatus() == ChildrenConstant.ChildrenStatus.DROP_OUT.codeOf() || children.getStatus() == ChildrenConstant.ChildrenStatus.GRADUATED.codeOf()))
            return StatusResponse.error(ErrorCode.DROP_OUT_OR_GRADUATE, "已毕业和已退学宝宝不能修改课时！");
        Boolean result = childrenCourseService.modifyLeftCourseTime(ticket, childrenId, remark, times);
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "修改剩余课时",httpMethod = "POST")
    @RequestMapping("/change_record")
    public StatusResponse changeRecord(String childrenId) {
        List<CourseChangeRecordVo> recordList = childrenCourseService.getRecordsByChildren(childrenId);
        return StatusResponse.success(recordList);
    }

}
